
  <tr>

  <td width="16%" valign="top">
     <p><div class="active_menu"><div class="menu"><a href="">Rybalka.com.ua</a></div></div></p>
     <p>(044) 460-49-87</p>
     <p><a href="/"><small>Доставка и оплата</small></a></p>
  </td>

<?php

  					if(isset ($level1)){ //Первый уровень
								if(isset ($level2)){ //Второй уровень
										if(isset ($level3)){ //Третий уровень
													if(isset ($level4)){ //Четвертый уровень

													    $query = $this->db->query('SELECT link, name,id  FROM store WHERE level=1 AND folder=1 ORDER BY list');

			                                  echo '<td width="16%" valign="top">';

			                                  	$i=0;
			                                    foreach ($query->result() as $row){
			                                    	$i++;

			                                          if($row->link == $level1){
			                                            $ii=$i;
			                                            echo '<div class="menu"><a href="/store/catalog/'. $row->link .'/">'. $row->name .'</a> <img src="/images/global/menu.gif" align="absmiddle"></div>';
			                                          }else{
			                                            echo '<div class="menu"><a href="/store/catalog/'. $row->link .'/">'. $row->name .'</a></div>';
			                                          }

			                                    }
			                                  echo '</td>';


                                 			$query = $this->db->query("SELECT link, name,id, folder  FROM store WHERE level=2 AND father=(SELECT id FROM store WHERE link='$level1') AND folder=1 ORDER BY list");


				                                  echo '<td width="16%" valign="top">';

		                                        for ($i = 2; $i <= $ii; $i++) {
		                                            echo '<div class="menu">&nbsp;</div>';
		                                        }

		                                    $i=0;
		                                    foreach ($query->result() as $row){
		                                    $i++;

		                                          if($row->link == $level2){
		                                          $ii2=$i+$ii;
		                                            echo '<div class="secondary_menu"><nobr><a href="/store/catalog/'. $level1 .'/'. $row->link .'/">'. $row->name .'</a>  <img src="/images/global/menu.gif" align="absmiddle"></nobr></div>';
		                                          }else{
		                                            echo '<div class="secondary_menu"><a href="/store/catalog/'. $level1 .'/'. $row->link .'/">'. $row->name .'</a></div>';
		                                          }

		                                    }
			                                  echo '</td>';



                              				 $query = $this->db->query("SELECT link, name,id, folder FROM store WHERE level=3 AND father=(SELECT id FROM store WHERE link='$level2'  limit 1) AND folder=1 ORDER BY list");

                                 			 echo '<td width="16%" valign="top">';

                                        	for ($i = 2; $i <= $ii; $i++) {
                                            	echo '<div class="menu">&nbsp;</div>';
                                        	}
                                        	
                                        	for ($i = 4; $i <= $ii2; $i++) {
                                            	echo '<div class="secondary_menu">&nbsp;</div>';
                                            
                                        	}

		                                    foreach ($query->result() as $row){

		                                               if($row->link == $level3){
		                                                 echo '<div class="secondary_menu"><a href="/store/catalog/'. $level1 .'/'. $level2 .'/'. $row->link .'/">'. $row->name .'</a></div>';
		                                                   }else{
		                                                echo '<div class="secondary_menu"><a href="/store/catalog/'. $level1 .'/'. $level2 .'/'. $row->link .'/">'. $row->name .'</a></div>';
		                                               }
		                                    }
			                                echo '</td>';


		                                     ?>
                                          <td width="16%" valign="top"></td>
		                                      <td width="16%" valign="top"></td>
                                         <?




											}else{ //Если только третий уровень


												$query = $this->db->query('SELECT link, name,id  FROM store WHERE level=1 AND folder=1 ORDER BY list');

				                                  echo '<td width="16%" valign="top">';

				                                    $i=0;
				                                    foreach ($query->result() as $row){
				                                    $i++;

				                                          if($row->link == $level1){
				                                            $ii=$i;
				                                            echo '<div class="menu"><a href="/store/catalog/'. $row->link .'/">'. $row->name .'</a> <img src="/images/global/menu.gif" align="absmiddle"></div>';
				                                          }else{
				                                            echo '<div class="menu"><a href="/store/catalog/'. $row->link .'/">'. $row->name .'</a></div>';
				                                          }

				                                    }
				                                  echo '</td>';


                          					    $query = $this->db->query("SELECT link, name,id, folder  FROM store WHERE level=2 AND father=(SELECT id FROM store WHERE link='$level1') AND folder=1 ORDER BY list");

			                                  echo '<td width="16%" valign="top">';

			                                        for ($i = 2; $i <= $ii; $i++) {
			                                            echo '<div class="menu">&nbsp;</div>';
			                                        }

			                                    $i=0;
			                                    foreach ($query->result() as $row){
			                                    $i++;

			                                          if($row->link == $level2){
			                                             $ii2=$i+$ii;
			                                            echo '<div class="secondary_menu"><a href="/store/catalog/'. $level1 .'/'. $row->link .'/">'. $row->name .'</a> <img src="/images/global/menu.gif" align="absmiddle"></div>';
			                                          }else{
			                                            echo '<div class="secondary_menu"><a href="/store/catalog/'. $level1 .'/'. $row->link .'/">'. $row->name .'</a></div>';
			                                          }

			                                    }
			                                  echo '</td>';


				                                $query = $this->db->query("SELECT link, name,id, folder  FROM store WHERE level=3 AND father=(SELECT id FROM store WHERE link='$level2' AND father=(SELECT id from store WHERE link='$level1')) AND folder=1 ORDER BY list");

				                                  echo '<td width="16%" valign="top">';

			                                         for ($i = 3; $i <= $ii; $i++) {
			                                            echo '<div class="menu">&nbsp;' . $i . '</div>';
			                                         }
                                              
                                               for ($i = $ii; $i <= $ii2; $i++) {
			                                            echo '<div class="secondary_menu">&nbsp;' . $i . '</div>';
			                                         }

				                                    foreach ($query->result() as $row){

				                                          if($row->link == $level3){
				                                               echo '<div class="secondary_menu">'. $row->name .'</div>';
				                                                   }else{
				                                                echo '<div class="secondary_menu"><a href="/store/catalog/'. $level1 .'/'. $level2 .'/'. $row->link .'/">'. $row->name .'</a></div>';
				                                               }
				                                    }
				                                echo '</td>';


				                                  ?> <td width="16%" valign="top"></td>
				                                      <td width="16%" valign="top"></td>

                                            <?

											}


										}else{ //Если только второй уровень

								        $query = $this->db->query('SELECT link, name,id  FROM store WHERE level=1 AND folder=1 ORDER BY list');

                                        echo '<td width="16%" valign="top">';

			                                   	$i=0;
			                                    foreach ($query->result() as $row){
			                                    $i++;

			                                          if($row->link == $level1){
			                                            $ii=$i;
			                                            echo  '<div class="menu"><a href="/store/catalog/'. $row->link .'/">'. $row->name .'</a> <img src="/images/global/menu.gif" align="absmiddle"></div>';
			                                          }else{
			                                            echo '<div class="menu"><a href="/store/catalog/'. $row->link .'/">'. $row->name .'</a></div>';
			                                          }

			                                    }
			                                  echo '</td>';


			                                  $query = $this->db->query("SELECT link, name,id, folder  FROM store WHERE level=2 AND father=(SELECT id FROM store WHERE link='$level1') AND folder=1 ORDER BY list");

			                                  echo '<td width="16%" valign="top">';

			                                        for ($i = 2; $i <= $ii; $i++) {
			                                            echo '<div class="menu">&nbsp;</div>';
			                                        }


			                                    $i = 0;
			                                    foreach ($query->result() as $row){
			                                    $i++;
			                                            if($row->link == $level2){
			                                                $ii2=$i+$ii-2;
			                                              echo '<div class="secondary_menu"><nobr>'. $row->name .' <img src="/images/global/menu.gif" align="absmiddle"></nobr></div>';
			                                              }else{
			                                              echo '<div class="secondary_menu"><a href="/store/catalog/'. $level1 .'/'. $row->link .'/">'. $row->name .'</a></div>';
			                                              }

			                                    }
			                                  echo '</td>';


			                                  $query = $this->db->query("SELECT link, name,id, folder  FROM store WHERE level=3 AND father=(SELECT id FROM store WHERE link='$level2'  AND father=(SELECT id from store WHERE link='$level1')) AND folder=1 ORDER BY list");

			                                  echo '<td width="16%" valign="top">';

			                                     
                                             
                                             for ($i = 2; $i <= $ii; $i++) {
			                                            echo '<div class="menu">&nbsp;' . $i . '</div>';
			                                        }
			                                        
			                                         for ($i = $ii; $i <= $ii2; $i++) {
			                                            echo '<div class="secondary_menu">&nbsp;' . $i . '</div>';
			                                        }

			                                    foreach ($query->result() as $row){
			                                      echo '<div class="secondary_menu"><a href="/store/catalog/'. $level1 .'/'. $level2 .'/'. $row->link .'/">'. $row->name .'</a></div>';
			                                      }

			                                    echo '</td>';

			                                  ?> <td width="16%" valign="top"></td>
                                            <td width="16%" valign="top"></td>
                                       
                                        
                                       <?


										}



									}else{ //Если только первый уровень

									$query = $this->db->query('SELECT link, name,id  FROM store WHERE level=1 AND folder=1');

                                    echo '<td width="16%" valign="top">';

                                 	$i=0;
                                    foreach ($query->result() as $row){
                                    $i++;


                                        if($row->link == $level1){
                                            $ii=$i;
                                            echo '<div class="menu">'. $row->name .' <img src="/images/global/menu.gif" align="absmiddle"></div>';

                                        }else{
                                            echo '<div class="menu"><a href="/store/catalog/'. $row->link .'/">'. $row->name .'</a></div>';
                                          	}

                                    }
		                                  echo '</td>';


		                                  $query = $this->db->query("SELECT link, name,id  FROM store WHERE level=2 AND father=(SELECT id FROM store WHERE link='$level1') AND folder=1 ORDER BY list");

		                                  echo '<td width="16%" valign="top">';


		                                        for ($i = 2; $i <= $ii; $i++) {
		                                            echo '<div class="menu">&nbsp;</div>';

		                                        }

		                                        foreach ($query->result() as $row){
		                                            echo '<div class="secondary_menu"><a href="/store/catalog/'. $level1 .'/'. $row->link .'/">'. $row->name .'</a></div>';
		                                        }



		                                  echo '</td>';

		                                  ?>
		                                  <td width="16%" valign="top">&nbsp;</td>
                                      <td width="16%" valign="top">&nbsp;</td>
                                      <td width="16%" valign="top">&nbsp;</td>
                                      <?

									}


					}else{ //Нулевой уровень


					  $query = $this->db->query("SELECT link, name FROM store WHERE level=1 AND folder=1 ORDER BY list");

			            echo '<td width="16%"  valign="top">';
			              foreach ($query->result() as $row){
			                   echo '<div class="menu"><a href="/store/catalog/'. $row->link .'/">'. $row->name .'</a></div>';
			              }
			            echo '</td>';


			             ?>
			              <td width="16%" valign="top">&nbsp;</td>
			              <td width="16%" valign="top">&nbsp;</td>
                    <td width="16%" valign="top">&nbsp;</td>
                    <td width="16%" valign="top">2</td>
                     <?

					}



?>


  </tr>
</table>